package io.renren.modules.user.util;

import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.DateUtils;
import io.renren.modules.api.entity.UsertokenEntity;
import io.renren.modules.dictionary.dao.DictionaryDao;
import io.renren.modules.dictionary.entity.DictionaryEntity;
import io.renren.modules.user.dao.UserinfosDao;
import io.renren.modules.user.entity.UserinfosEntity;
import io.renren.modules.user.service.UserinfosService;
import io.renren.modules.vipuser.dao.VipinfosDao;
import io.renren.modules.vipuser.entity.VipinfoCustomEntity;
import io.renren.modules.vipuser.util.VipUserUtil;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Title: stf
 * @Description: 用户工具类
 * @Author: 杨帅
 * @CreateDate: 2018/10/21 19:21
 * @Version: 1.0
 */
public class UserUtil {

    /*
    * 时间换算
    * */
    public static String timeConversion(String dateStr){
        switch (dateStr){
            case "1":
                return "1天";
            case "31":
                return "1月";
            case "365":
                return "1年";
            default:
        }
        return null;
    }


    public static Map<String,Object> passwordErrorFieldDictionaryInformationProcessing(List<DictionaryEntity> list) {
        Map<String, Object> map = new HashMap<>(16);
        for (DictionaryEntity dictionaryEntity : list) {
            switch (dictionaryEntity.getDatatypename()) {
                case "UserFailedPasswordAttemptCount":
                    //错误密码次数更新参照
                    map.put("UserFailedPasswordAttemptCount", Integer.parseInt(dictionaryEntity.getDictionaryvalue()));
                    break;
                case "PasswordInterval":
                    //错误密码次数时间更新参照（分钟）
                    map.put("PasswordInterval", Integer.parseInt(dictionaryEntity.getDictionaryvalue()));
                    break;
                default:
            }
        }
        return map;
    }

    public static String initialIntegrationProcessing(List<DictionaryEntity> list) {
        if (list != null){
            for (DictionaryEntity dictionaryEntity : list) {
                if ("IntegralNumber".equals(dictionaryEntity.getDatatypename())){
                    return dictionaryEntity.getDictionaryvalue();
                }
            }
        }
        return null;
    }


    /**
     * @methodName vipExpiration
     * @param vipinfosDao
     * @param userinfosDao
     * @param userinfoId
     * @throws Exception
     * @return boolean true:vip用户,false:普通用户
     * @author 杨帅
     * @date 2018/11/1 8:46
     *判断是否vip，并且是否过期，如果过期，则设置相应身份状态
     */
    public static boolean isvip(VipinfosDao vipinfosDao,
                                UserinfosDao userinfosDao,
                                String userinfoId){

        UserinfosEntity userinfosEntity = userinfosDao.queryObject(userinfoId);
        if (ConstantUtils.COMMON.equals(userinfosEntity.getIsvip())){
            return false;
        }else{
            VipinfoCustomEntity vipinfoCustomEntity = vipinfosDao.queryVip(userinfoId);
            //判断会员是否到期
            if (vipinfoCustomEntity.getExpirationtime().getTime() < System.currentTimeMillis()) {
                //到期，设置用户状态为普通用户状态
                UserinfosEntity entity = new UserinfosEntity();
                entity.setUserinfoid(userinfoId);
                entity.setIsvip(ConstantUtils.COMMON);
                userinfosDao.update(entity);
                return false;
            }
            //vip没有过期
            return true;
        }
    }

}
